package com.example.hasee.zhcd.Activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.NetUtil.HttpUtils;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;
import com.example.hasee.zhcd.Util.UpdateManager;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import static com.example.hasee.zhcd.Util.GlobalParameter.put;

/**
 * Created by hasee on 2018/5/9.
 */

public class WelcomeActivity extends BaseActivity {
    private String yhm;
    private String pwd;
    private String yunyingshang;
    private LinearLayout tiaoguo;
    private TimerTask task;
    private Timer timer;
    private String yysflag;
    private String token="";
    private UpdateManager mUpdateManager;
    private String strIMEI;

    @Override
    public int contentViewId() {
        return R.layout.activity_welcome;
    }

    @Override
    public void set() {
        PushAgent mPushAgent = PushAgent.getInstance(WelcomeActivity.this);
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回device token
                token = deviceToken;
                Log.e("我是爸爸啊", token);
            }

            @Override
            public void onFailure(String s, String s1) {

            }
        });
        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
            @Override
            public void dealWithCustomAction(Context context, UMessage msg) {
                Toast.makeText(context, "haha", Toast.LENGTH_LONG).show();
            }
        };
        mPushAgent.setNotificationClickHandler(notificationClickHandler);
    }

    @Override
    public void init() {
//        TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
//        strIMEI=tm.getDeviceId();
    }

    public String getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "9.0.0";
        }
    }

    private void getLogin() {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strAccount", yhm);
        map.put("strPassword", pwd);
        map.put("strTOKEN", token);
        map.put("strIMEI",strIMEI);
        httpUtils.get("ZH_Login", map, "ZH_Login");
    }

    private void getUpdata() {
        ProgressDialogUtils.createProgressDialog(WelcomeActivity.this, "正在获取更新信息...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("version", getVersion());
        map.put("strFlag", yysflag);
        httpUtils.get("ZHUpdate", map, "ZHUpdate");
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "ZH_Login":
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.get("rspcode").toString();
                    String rspmessage = json.getString("rspmessage");
                    if ("3".equals(rspcode)) {
                        JSONObject data = json.getJSONObject("data");
                        HashMap<String, String> loginfomap = new HashMap<String, String>();
                        //{"user":"许洪霞", "role":"", "account":"805", "user":"5530"}
                        loginfomap.put("user", data.getString("user"));
                        loginfomap.put("role", data.getString("role"));
                        loginfomap.put("account", data.getString("account"));
                        loginfomap.put("phone", yhm);
                        put("loginmap", loginfomap);
                        GlobalParameter.putString("rspcode", rspcode);
                        Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                        intent.putExtra("token", token);
                        startActivity(intent);
                        WelcomeActivity.this.finish();
                    } else {
                        Toast.makeText(WelcomeActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                        timer.cancel();
                        Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                        intent.putExtra("token", token);
                        startActivity(intent);
                        WelcomeActivity.this.finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "ZHUpdate":
                Log.v("ZHUpdate", msg.getMsg());
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.get("rspcode").toString();
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        String downurl = json.getString("downloadurl");
                        mUpdateManager = new UpdateManager(WelcomeActivity.this);
                        mUpdateManager.checkUpdateInfo(downurl);
                    } else if ("1".equals(rspcode)) {
                        if ("".equals(yhm)) {
                            timer.schedule(task, 1000 * 3);
                            tiaoguo = (LinearLayout) findViewById(R.id.tiaoguo);
                            tiaoguo.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View arg0) {
                                    // TODO Auto-generated method stub

                                    if ("".equals(yhm)) {
                                        timer.cancel();
                                        Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                                        intent.putExtra("token", token);
                                        startActivity(intent);
                                        WelcomeActivity.this.finish();
                                    } else {
                                        timer.cancel();
                                        Log.e("欢迎页的token", token);
                                        getLogin();
                                    }
                                }
                            });
                        } else {
                            Log.e("欢迎页的token", token);
                            getLogin();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
        strIMEI=tm.getDeviceId();
        yunyingshang = tm.getSimOperator();
        if ("46003".equals(yunyingshang)) {
            HttpUtils.service_url = HttpUtils.dianxin_url;
            yysflag = "电信";
        } else {
            HttpUtils.service_url = HttpUtils.liantong_url;
            yysflag = "联通";
        }
        SharedPreferences preferences = getSharedPreferences("userInfo",
                Activity.MODE_PRIVATE);
        yhm = preferences.getString("yhm", "");
        pwd = preferences.getString("pwd", "");
        token = preferences.getString("token", "");
        Log.e("flag", yysflag);
        getUpdata();
        final Intent it = new Intent(WelcomeActivity.this, LoginActivity.class); // 你要转向的Activity
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                if ("".equals(yhm)) {
                    it.putExtra("token", token);
                    startActivity(it);
                    WelcomeActivity.this.finish();
                } else {
                    Log.e("欢迎页的token", token);
                    getLogin();
                }
            }
        };
    }
}
